using System;
using PHSRAG.Insight21.Policy;

namespace PHSRAG.Insight21.Common.Pages
{
    /// <summary>
    /// This class is designed to act as a base for all Insight pages using caching.
    /// </summary>
    public class CachePage : BasePage
    {
        #region Private instance variables

        private ICachePolicy _cachePolicy;
        private string _pageCacheKey;
        private ICachePolicy _pageCachePolicy;
        private ICachePolicy _viewStateCachePolicy;

        #endregion

        #region Properties

        /// <summary>
        /// Key into the page cache
        /// </summary>
        public string PageCacheKey
        {
            get { return _pageCacheKey; }
            set { _pageCacheKey = value; }
        }

        /// <summary>
        /// Gets the current Cache Policy
        /// </summary>
        public ICachePolicy PrevailingCachePolicy
        {
            get { return _cachePolicy; }
        }

        /// <summary>
        /// Gets the current page cache policy
        /// </summary>
        public ICachePolicy PrevailingPageCachePolicy
        {
            get { return _pageCachePolicy; }
        }

        /// <summary>
        /// Gets the current page cache policy
        /// </summary>
        public ICachePolicy PrevailingViewStateCachePolicy
        {
            get { return _viewStateCachePolicy; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Default Constructor - Required for the design time view of the aspx page
        /// </summary>
        public CachePage()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="cacheKey">Page Cache Key</param>
        public CachePage(string cacheKey)
        {
            _pageCacheKey = cacheKey;
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Page Initialization
        /// </summary>
        /// <param name="e">Arguments associated with the event</param>
        protected override void OnInit(EventArgs e)
        {
            _cachePolicy = (ICachePolicy) Session[Names.CachePolicyKey];
            _pageCachePolicy = (ICachePolicy) Session[Names.CachePolicyKey];
            _viewStateCachePolicy = new ViewStateCachePolicy(ViewState);
            base.OnInit(e);
        }

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="e">Arguments associated with the event</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (string.IsNullOrEmpty(_pageCacheKey))
            {
                throw new Exception("PageCacheKey is not set.");
            }
        }

        #endregion
    }
}